#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

#define LED_B 6
#define LED_G 9
#define LED_R 3
#define SPECTRUM_STROBE 4
#define SPECTRUM_RESET 5

int ledHSV[3];
byte ledRGB[3];
/*
Ethernet utilise normalement
4 - 10 - 11 - 12 - 13
>> Le 4 est pour la carte SD, éviter peut-etre d'en mettre une en place
*/ 

void setup(){
  
  Serial.begin(115200);
  
  //setting pin mode
  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(SPECTRUM_STROBE, OUTPUT);
  pinMode(SPECTRUM_RESET, OUTPUT);
  
  
  //init color
  ledHSV[0] = 0;
  ledHSV[1] = 100;
  ledHSV[2] = 0;
  
  hsv2rgb(ledHSV, ledRGB);
  
  //init spectrum
  analogReference(EXTERNAL);
  initSpectrum();
  
  
  //UDP
  initUdp();
  
}

void loop(){
  
  //spectrum
  float amplitude = getHigherAmplitude();
  amplitude = getWeightedAmplitude();
  //amplitude /=2;
  Serial.
  Serial.println(amplitude);
  amplitude = filterAmplitude(amplitude);
  
  ledHSV[2] = amplitude * 100;
  hsv2rgb(ledHSV, ledRGB);
  Serial.println(amplitude);
  
  analogWrite(LED_R, ledRGB[0]);
  analogWrite(LED_G, ledRGB[1]);
  analogWrite(LED_B, ledRGB[2]);
  
  sendLedValue((char)(amplitude * 255.0)); 
  
   delay(20);
}
